<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/8/30
 * Time: 14:40
 */

use yii\helpers\Url;

?>
<script type="application/javascript">
    var check_goods_list = [];
    $(function ($) {
        <?php
        if($item != null){
        if($item['page_image_url'] != ''){
        ?>
        $("#page_image").show();
        <?php
        }
        }
        ?>
        $('#image_file').change(function (event) {
            var files = event.target.files, file;
            if (files && files.length > 0) {
                file = files[0];
                console.log(file);
                if (file.size > 1024 * 1024 * 2) {
                    alert('图片大小不能超过 2MB!');
                    return false;
                }
                var URL = window.URL || window.webkitURL;
                var imgURL = URL.createObjectURL(file);
                $("#page_image").attr("src", imgURL);
                $("#page_image").show();
            }
        });


        // 初始化选中的商品
        var goodsChecks = $("#goods_table").find('input:checkbox[name=goods]:checked');
        for (var i = 0; i < goodsChecks.length; i++) {
            var g = {};
            g.code = $(goodsChecks[i]).val();
            g.sort = $(goodsChecks[i]).parent().prev().find('input').val();
            check_goods_list.push(g);
        }
    });

    function checkSort(obj) {
        var sortTextBox = $(obj).parent().prev().find('input');
        if ($(obj).is(':checked')) {
            sortTextBox.removeAttr("disabled");
        } else {
            sortTextBox.attr("disabled", "disabled");
        }
    }

    function intChange(filter) {
        if (event.keyCode == 13 || filter != undefined) {
            var data = {};

            data.name = $("#name").val();
            data.description = $("#description").val();
            data.id = $("#id").val();
            data.integral = $("#integral").val();
            data.check_goods_list = check_goods_list;
            if (data.integral == '' || data.integral < 0) {
                return false;
            }

            $("#modaledit").html('');

            loading.show();
            requestHtml('card-tpl-part-view', data, true, function (responseHtml) {
                $("#modaledit").html(responseHtml);
                loading.hide();
            });
        }
    }

    function int_filter() {
        intChange(true);
    }

    function chkChange(obj) {
        var isChecked = $(obj).is(':checked');
        if (isChecked) {
            var g = {};
            g.code = $(obj).val();
            g.sort = $(obj).parent().prev().find('input').val();
            check_goods_list.push(g);
        } else {
            var code = $(obj).val();
            for (var i = 0; i < check_goods_list.length; i++) {
                if (check_goods_list[i]['code'] === code) {
                    check_goods_list.splice(i, 1);//从下标为i的元素开始，连续删除1个元素
                    i--;//因为删除下标为i的元素后，该位置又被新的元素所占据，所以要重新检测该位置
                }
            }
        }
    }

    function sortChange(obj) {
        var sort = $(obj).val();
        var code = $(obj).parent().parent().find('input[name="goods"]').val();
        for (var i = 0; i < check_goods_list.length; i++) {
            if (check_goods_list[i]['code'] === code) {
                check_goods_list[i]['sort'] = sort;
            }
        }
    }

</script>
<div>
    <form class="form-horizontal" id="modaledit_submit_form" method="post" enctype="multipart/form-data"
          action="card-tpl-save" target="upload_exec_target">

        <div class="form-group" style=" margin-top: 5px">
            <input type="hidden" id="id" name="id" class="form-control" value="<?= $item != null ? $item['id'] : '' ?>">
            <label class="control-label col-lg-2" class="control-label" for="exampleInputPassword2">卡片名称：</label>
            <div class="col-lg-10">
                <input type="text" id="name" name="name" class="form-control"
                       value="<?= $name != '' ? $name : ($item != null ? $item['name'] : '') ?>">
            </div>
        </div>


        <div class="form-group" style=" margin-top: 5px">
            <label class="control-label col-lg-2" class="control-label" for="exampleInputPassword2">卡片描述：</label>
            <div class="col-lg-10">
            <textarea id="description" name="description" class="form-control"
                      rows="3"><?= $description != '' ? $description : ($item != null ? $item['description'] : '') ?></textarea>
            </div>
        </div>
        <div class="form-group" style=" margin-top: 5px">
            <label class="control-label col-lg-2" class="control-label" for="exampleInputPassword2">价值积分：</label>
            <div class="col-lg-10">
                <div class="input-group m-bot15">
                    <input type="number" id="integral" name="integral" class="form-control"
                           value="<?= $integral != 0 ? $integral : ($item != null ? $item['integral'] : '') ?>"
                           onkeypress="intChange()">
                    <span class="input-group-btn">
                                                <button class="btn btn-primary" type="button"
                                                        onclick="int_filter()">筛选</button>
                                              </span>
                </div>
            </div>
        </div>
        <div class="form-group" style=" margin-top: 2px">
            <label class="control-label col-lg-2" class="control-label" for="exampleInputPassword2">模板封面：</label>
            <div class="col-lg-10">
                <input id="image_file" name="image_file" type="file" accept="image/*" class="form-control">
                <img id="page_image" name="page_image" src="<?= $item != null ? $item['page_image_url'] : '' ?>"
                     class="img-responsive" style="display: none;width:150px;"/>
            </div>
        </div>
        <div class="form-group" style=" margin-top: 5px;">
            <div class="col-lg-12">
                <input type="hidden" id="card_tpl_goods" name="card_tpl_goods">
                <table class="table table-bordered table-hover table-condensed" id="goods_table">
                    <thead class="nowrap">
                    <tr>
                        <th class="col-lg-1">排序(数字大的排前面)</th>
                        <th class="col-lg-1"></th>
                        <th class="col-lg-2">产品编码</th>
                        <th class="col-lg-6">产品名</th>
                        <th class="col-lg-2">价格</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    if ($goods != null && $goods != '') {
                        for ($i = 0; $i < count($goods); $i++) {
                            ?>
                            <tr>
                                <td><input type="number" <?= $goods[$i]['sort'] == null ? 'disabled' : '' ?>
                                           value="<?= $goods[$i]['sort'] == null ? 0 : $goods[$i]['sort'] ?>"
                                           oninput="sortChange(this)"></td>
                                <td><input onclick="checkSort(this)" type="checkbox" onchange="chkChange(this)"
                                           name="goods" <?= $goods[$i]['sort'] != null ? 'checked="checked"' : '' ?>
                                           value="<?= $goods[$i]['code'] ?>"></td>
                                <td><?= $goods[$i]['code'] ?></td>
                                <td><?= $goods[$i]['name'] ?></td>
                                <td><?= $goods[$i]['selling_price'] ?></td>
                            </tr>
                            <?php
                        }
                    }
                    ?>
                    </tbody>
                </table>
            </div>
        </div>
    </form>
    <iframe id="upload_exec_target" name="upload_exec_target" style="display: none;"></iframe>
</div>
